/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.yonsei.iwb.sdt.refinement;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import java.util.Vector;
import org.yonsei.iwb.sdt.ontology.PropertyArc;

/**
 *
 * @author JDK
 */
public class DomainRestrictionRefinement extends Refinement {

    private String name = new String();
    private ExpandingHistory eh;
    private String corename = new String();
//    public DomainRestrictionRefinement(RDFNode standard, String prefix, Property prop) {
//        this.name = prop.getLocalName().toString() +".(" + prefix + standard.asLiteral().getLexicalForm() + ")";
//    }
    
    public DomainRestrictionRefinement(String name, String corename,ExpandingHistory eh) {
        this.name = name;
        this.eh = eh;
        this.corename= corename;
        super.refinementType = RefinementType.DOMAINRESTRICTION;
    }
    
    public DomainRestrictionRefinement(String porne, RDFNode standard, String prefix, Property prop) {
        this.name = porne + prop.getLocalName().toString() +".(" + prefix + standard.asLiteral().getLexicalForm() + ")";
        this.corename = porne + prop.getLocalName().toString() +".(" + prefix + standard.asLiteral().getLexicalForm() + ")";
        this.cc = new ConstraintConstructor(prefix, standard.asLiteral().getLexicalForm(), prop);
        super.refinementType = RefinementType.DOMAINRESTRICTION;
    }
    
    public DomainRestrictionRefinement(RDFNode standard, String prefix, ExpandingHistory eh, Property prop) {
        // 안쓰는듯;;;;
        this.eh = eh;
        Vector<String> quantifiers = this.eh.getQuantifierSet();
        Vector<PropertyArc> propertySet = this.eh.getPropertySet();
        
        //여기에 가장 안쪽 .( >= 22)
        this.name = prop.getLocalName().toString() +".(" + prefix + standard.asLiteral().getLexicalForm() + ")";
        this.corename = prop.getLocalName().toString() +".(" + prefix + standard.asLiteral().getLexicalForm() + ")";
        this.cc = new ConstraintConstructor(prefix, standard.asLiteral().getLexicalForm(), prop);
        
        for (int i = 0; i < propertySet.size(); i++) {
            PropertyArc pa = propertySet.get(i);
            Property p = pa.getProperty();
            String q = quantifiers.get(i);
            if(pa.getDirection()){
                this.name = q+" "+p.getLocalName()+".("+this.name+")";                
            }else{
                this.name = q+" "+p.getLocalName()+"￣.("+this.name+")";                
            }
            
        }
        super.refinementType = RefinementType.DOMAINRESTRICTION;
                
    }
    
    public DomainRestrictionRefinement(Vector<RDFNode> standardSet, Property prop){
        String cons = new String();        
        for (int i = 0; i < standardSet.size(); i++) {
            RDFNode r = standardSet.get(i);
            if(i!=standardSet.size()-1){                
                cons += r.asLiteral().getLexicalForm().toString() + DLConstructors.CONJUNCTION;
            }else{
                cons += r.asLiteral().getLexicalForm().toString();
            }
        }        
        this.name = prop+".(!=("+ cons +"))";
        this.corename = prop+".(!=("+ cons +"))";
        this.cc = new ConstraintConstructor(cons);
        super.refinementType = RefinementType.DOMAINRESTRICTION;
    }
    
    public DomainRestrictionRefinement(String porne, RDFNode standard, String prefix, ExpandingHistory eh, Property prop) {
        this.eh = eh;
        Vector<String> quantifiers = this.eh.getQuantifierSet();
        Vector<PropertyArc> propertySet = this.eh.getPropertySet();
        
        //여기에 가장 안쪽 .( >= 22)
        this.name = prop.getLocalName().toString() +".(" + prefix + standard.asLiteral().getLexicalForm() + ")";
        this.corename = prop.getLocalName().toString() +".(" + prefix + standard.asLiteral().getLexicalForm() + ")";
        this.cc = new ConstraintConstructor(prefix, standard.asLiteral().getLexicalForm(), prop);
        
        for (int i = 0; i < propertySet.size(); i++) {            
            PropertyArc pa = propertySet.get(i);
            Property p = pa.getProperty();
            String q = quantifiers.get(i);
            if(pa.getDirection()){
                this.name = q+" "+p.getLocalName()+".("+this.name+")";                
            }else{
                this.name = q+" "+p.getLocalName()+"￣.("+this.name+")";                
            }
        }
        
        this.name = porne + this.name;
        super.refinementType = RefinementType.DOMAINRESTRICTION;
    }
    
    public ExpandingHistory getExpandingHistory(){
        return this.eh;
    }

    @Override
    public void generateRefinementName() {
        super.refinement_name = this.name;
    }
    
    @Override
    public Refinement generateUpdatedRefinement(Vector<Resource> instanceSet){
        Refinement ref = new DomainRestrictionRefinement(this.name, this.corename, this.eh);
        ref.setBranchingType(this.branching_type);
        ref.setConstraintConstructor(this.cc);
        Vector<Resource> oldinsSet = this.getInstanceSet();        
        for (int i = 0; i < instanceSet.size(); i++) {
            Resource r = instanceSet.get(i);
            if(oldinsSet.contains(r)){
                ref.setInstanceSet(r);
            }
        }
//        private boolean refinement_type = true;
//        protected String refinement_name;
//        private Vector<Resource> instanceSet;
        return ref;
    }
    
    @Override
    public String getCoreName() {
        return this.corename;
    }

    @Override
    public Vector<Resource> getConstraintMatcherInstance(Model model) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String getName() {
        return this.name;
    }
}
